package com.dingxin.ship_together_api.config.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    public static final String DELAY_EXCHANGE_NAME = "ship-together.business.exchange";//普通的交换机
    public static final String DELAY_QUEUE_NAME = "ship-together.business.order.queue";//声明队列
    public static final String DELAY_QUEUE_ROUTING_KEY = "ship-together.business.order.rk";

    public static final String DEAD_LETTER_EXCHANGE = "ship-together.business.dead.exchange";//Dead Letter Exchanges
    public static final String DEAD_LETTER_QUEUE_NAME = "ship-together.business.order.dead.queue";
    public static final String DEAD_LETTER_QUEUE_ROUTING_KEY = "ship-together.business.order.dead.rk";

    // 声明延时Exchange
    @Bean("delayExchange")
    public DirectExchange delayExchange() {
        return new DirectExchange(DELAY_EXCHANGE_NAME);
    }

    // 声明死信Exchange
    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(DEAD_LETTER_EXCHANGE);
    }

    // 声明延时队列 延时30m
    // 并绑定到对应的死信交换机
    @Bean("delayQueue")
    public Queue delayQueueA() {
        Map<String, Object> args = new HashMap<>(2);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUE_ROUTING_KEY);
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl", 1000 * 60 * 30);
        return QueueBuilder.durable(DELAY_QUEUE_NAME).withArguments(args).build();
    }


    // 声明死信队列 用于接收延时的消息
    @Bean("deadLetterQueue")
    public Queue deadLetterQueueA() {
        return new Queue(DEAD_LETTER_QUEUE_NAME);
    }

    // 声明延时队列绑定关系
    @Bean
    public Binding delayBindingA(@Qualifier("delayQueue") Queue queue,
                                 @Qualifier("delayExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DELAY_QUEUE_ROUTING_KEY);
    }


    // 声明死信队列绑定关系
    @Bean
    public Binding deadLetterBindingA(@Qualifier("deadLetterQueue") Queue queue,
                                      @Qualifier("deadLetterExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_QUEUE_ROUTING_KEY);
    }

}
